package com.objectproject.main.service;

import java.util.ArrayList;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.objectproject.main.dao.MyPropertyDAOImpl;
import com.objectproject.main.model.MyObject;
import com.objectproject.main.model.MyProperty;
import com.objectproject.main.service.iface.MyPropertyService;
import com.objectproject.main.service.subsystem.MyLocaleResolver;
import com.objectproject.main.service.subsystem.utility.PropertyUtility;

@Service
public class MyPropertyServiceImpl implements MyPropertyService {
	
	@Autowired
	MyPropertyDAOImpl myPropertyDAOImpl;
	
	@Autowired
	MyLocaleResolver myLocaleResolver;
	
	@Autowired
	PropertyUtility propertyUtility;

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void addMyProperties(MyObject targetObject, ArrayList<MyProperty> myProperties, Locale locale) {
		for(MyProperty myProperty:myProperties){
			addMyProperty(targetObject, myProperty, locale);
		}
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void addMyProperty(MyObject targetObject, MyProperty myProperty, Locale locale) {
		// TODO Auto-generated method stub
		
		// 1. 프로퍼티의 값으로 오는 프리미티브 삽입(프로퍼티 입력전 선수작업)(객체는 이 수준에서 삽입x) Primitive Insertion
		long insertedId = 0L;
		if(myProperty.getValue() != null && myProperty.getValueId() == 0){
			insertedId = propertyUtility.insertMyPropertyValueEasy(myProperty.getValue());
			myProperty.setValueId(insertedId);
		}
		
		// 2. 프로퍼티 삽입
		myPropertyDAOImpl.insertMyProperty(targetObject, myProperty);
		
		// 3. 프로퍼티 상세 삽입
		//먼저 디테일 있는지 확인-------------------------------------------------
		myPropertyDAOImpl.insertMyPropertyDetail(targetObject, myProperty, myLocaleResolver.resolveLocaleId(locale));
	}

	

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void overrideMyProperty(MyObject targetObject, MyProperty myProperty, Locale locale) {
		
		//단순하게 addMyProperty 호출(오버라이딩 메소드 x 어차피 insert.. 에 구현되어있음)
		addMyProperty(targetObject, myProperty, locale);
		
	}

	@Override
	public void removeMyProperty(long objectId, long propertyId) {
		myPropertyDAOImpl.deleteMyPropertyOfById(objectId, propertyId);
	}
	
	

	@Override
	public void removeMyProperty(long propertyId) {
		myPropertyDAOImpl.deleteMyPropertyById(propertyId);
	}

	@Override
	public void reviseMyProperty(MyObject targetObject, MyProperty myProperty) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void reviseMyPropertyDetail(MyObject targetObject, MyProperty myProperty, Locale locale) {
		myPropertyDAOImpl.updateMyPropertyDetail(targetObject, myProperty, myLocaleResolver.resolveLocaleId(locale));
		
	}
	
	@Override
	public void reviseMyPropertyValue(MyObject targetObject, MyProperty myProperty, Locale locale) {
		// TODO Auto-generated method stub
		myPropertyDAOImpl.updateMyPropertyValue(targetObject, myProperty, myLocaleResolver.resolveLocaleId(locale));
	}

}
